package org.heydr.common;

import java.io.InputStream;
import java.util.Properties;

import org.heydr.common.filter.Filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置
 * @author carlos.tse
 *
 */
public class Config {
	private Logger logger = LoggerFactory.getLogger(Config.class);
	
	/* task crawler core thread num */
	private int taskCoreThreadpoolSize = 1;
	/* task crawler max thread num */
	private int taskMaxThreadpoolSize = 1;
	/* query data limit size */
	private int dbLimit = 100;
	/* frequency to flush available url into queue when more than one alarm received */
	private long taskEmptyAlarmFrequency = 2000;
	
	private Filter filter;
	
	public Config(){
	}
	
	public Config(String properties){
		addProperties(properties);
	}
	
	public Config(int taskCoreThreadpoolSize,int taskMaxThreadpoolSize){
		this.taskCoreThreadpoolSize = taskCoreThreadpoolSize;
		this.taskMaxThreadpoolSize = taskMaxThreadpoolSize;
	}
	
	/**
	 * description:manual init method
	 * @param path
	 * @return void
	 * @author carlos.xie
	 * 2013-5-21
	 */
	public void addProperties(String path){
		Properties properties = new Properties();
		InputStream in = ClassLoader.getSystemResourceAsStream(path);
		try {
			properties.load(in);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		int tcts = Integer.valueOf(properties.getProperty("task.core.threadpool.size"));
		setTaskCoreThreadpoolSize(tcts==0?5:tcts);
		int tmts = Integer.valueOf(properties.getProperty("task.max.threadpool.size"));
		setTaskMaxThreadpoolSize(tmts==0?5:tmts);
		int dblimit = Integer.valueOf(properties.getProperty("db.query.limit"));
		setDbLimit(dblimit==0?3000:dblimit);
		long teaf = Long.valueOf(properties.getProperty("task.empty.alarm.frequency"));
		setTaskEmptyAlarmFrequency(teaf==0?60000:teaf);
	}
	
	
	public int getTaskCoreThreadpoolSize() {
		return taskCoreThreadpoolSize;
	}

	public void setTaskCoreThreadpoolSize(int taskCoreThreadpoolSize) {
		this.taskCoreThreadpoolSize = taskCoreThreadpoolSize;
	}

	public int getTaskMaxThreadpoolSize() {
		return taskMaxThreadpoolSize;
	}

	public void setTaskMaxThreadpoolSize(int taskMaxThreadpoolSize) {
		this.taskMaxThreadpoolSize = taskMaxThreadpoolSize;
	}

	public long getTaskEmptyAlarmFrequency() {
		return taskEmptyAlarmFrequency;
	}

	public void setTaskEmptyAlarmFrequency(long taskEmptyAlarmFrequency) {
		this.taskEmptyAlarmFrequency = taskEmptyAlarmFrequency;
	}

	public int getDbLimit() {
		return dbLimit;
	}

	public void setDbLimit(int dbLimit) {
		this.dbLimit = dbLimit;
	}

	public Filter getFilter() {
		return filter;
	}

	public void setFilter(Filter filter) {
		this.filter = filter;
	}
	
}
